package frontend.user;

import backend.DiseaseItem;
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DiseaseListPanel extends JPanel {
    private final JList<String> diseaseList;
    private final DefaultListModel<String> listModel;
    private Map<String, DiseaseItem> diseaseMap;

    public DiseaseListPanel() {
        setLayout(new BorderLayout());
        setBackground(Color.LIGHT_GRAY);

        listModel = new DefaultListModel<>();
        diseaseList = new JList<>(listModel);
        diseaseList.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        diseaseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        diseaseList.setVisibleRowCount(-1); // 自适应高度

        JScrollPane scrollPane = new JScrollPane(diseaseList);
        scrollPane.setPreferredSize(new Dimension(200, 0)); // 设置宽度为200像素
        add(scrollPane, BorderLayout.CENTER);

        diseaseMap = new HashMap<>();
    }

    public void updateDiseaseList(List<DiseaseItem> diseases) {
        listModel.clear();
        diseaseMap.clear();

        if (diseases == null || diseases.isEmpty()) {
            return;
        }

        for (DiseaseItem disease : diseases) {
            String name = disease.Cause(); // 假设 Cause() 返回疾病名称
            listModel.addElement(name);
            diseaseMap.put(name, disease);
        }
    }

    public DiseaseItem getSelectedDisease() {
        String selectedValue = diseaseList.getSelectedValue();
        return diseaseMap.get(selectedValue);
    }

    public void addListSelectionListener(ListSelectionListener listener) {
        diseaseList.addListSelectionListener(listener);
    }
}
